package com.qingyun.video.service.impl;


import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qingyun.apis.user.IUserClient;
import com.qingyun.common.constants.VideoConstants;
import com.qingyun.model.common.dtos.R;
import com.qingyun.model.search.vos.SearchVo;
import com.qingyun.model.user.pojos.User;
import com.qingyun.model.video.pojos.Video;
import com.qingyun.model.video.pojos.VideoChannel;
import com.qingyun.video.mapper.ReplyMapper;
import com.qingyun.video.mapper.VideoChannelMapper;
import com.qingyun.video.mapper.VideoMapper;
import com.qingyun.video.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import utils.thread.AppThreadLocalUtil;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;



@Slf4j
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

    @Resource
    private VideoMapper videoMapper;
    @Resource
    private VideoChannelService videoChannelService;


    @Resource
    private ReplyService replyService;

    @Resource
    private LikeService likeService;


    @Resource
    private ReplyMapper replyMapper;

    @Resource
    private IUserClient userClient;
    @Resource
    private VideoChannelMapper videoChannelMapper;
    @Override
    public R<List<Video>> selectAllVideo() {
        List<Video> videos = query().eq("status",9).orderByDesc("likes").list();
        List<Video> list = videos.stream().map(video -> {
            video.setIsLike(likeService.isLike(video.getId()));
            LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) userClient.information(video.getAuthorId()).getData();
            User userInfoDto = JSONUtil.toBean(JSONUtil.toJsonStr(data), User.class);
            video.setAuthorName(userInfoDto.getName());
            video.setAuthorImage(userInfoDto.getImage());
            return video;
        }).collect(Collectors.toList());
        return R.success(list);
    }

    @Override
    public R<List<Video>> selectVideoByChannel(Long id) {
        List<Video> videos = query().eq("status",9).eq("channel_id", id).orderByDesc("likes").list();
        List<Video> list = videos.stream().map(video -> {
            video.setIsLike(likeService.isLike(video.getId()));
            LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) userClient.information(video.getAuthorId()).getData();
            User userInfoDto = JSONUtil.toBean(JSONUtil.toJsonStr(data), User.class);
            video.setAuthorName(userInfoDto.getName());
            video.setAuthorImage(userInfoDto.getImage());
            return video;
        }).collect(Collectors.toList());
        return R.success(list);
    }






    @Override
    public R saveVideo(Video video) {
        //检查参数
        if (video==null){
            return R.error("参数错误");
        }

        //判断是否存在id
        if (video.getId()==null){
            //不存在id 保存 文章 文章配置 文章内容
            save(video);

        }else {
            //存在id 修改 文章 文字内容
            updateById(video);

        }


//发送消息，创建索引
//        createVideoESIndex(video,video.getContent());

        //结果返回文章id
        return R.success(video.getId());
    }

    @Override
    public R<List<VideoChannel>> selectChannel() {
        List<VideoChannel> list = videoChannelMapper.selectList(null);
        return R.success(list);
    }

    @Override
    public R<List<Video>> selectMyVideo() {
        List<Video> videos = query().eq("author_id", AppThreadLocalUtil.getUser().getId()).list();
        List<Video> list = videos.stream().map(video -> {
            video.setIsLike(likeService.isLike(video.getId()));
            LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) userClient.information(video.getAuthorId()).getData();
            User userInfoDto = JSONUtil.toBean(JSONUtil.toJsonStr(data), User.class);
            video.setAuthorName(userInfoDto.getName());
            video.setAuthorImage(userInfoDto.getImage());
            return video;
        }).collect(Collectors.toList());
        return R.success(list);
    }

    @Override
    public R<List<Video>> selectMyVideoToBeReviewed() {
        List<Video> videos = query().eq("author_id", AppThreadLocalUtil.getUser().getId()).le("status",1).list();
        List<Video> list = videos.stream().map(video -> {

            video.setIsLike(likeService.isLike(video.getId()));
            LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) userClient.information(video.getAuthorId()).getData();
            User userInfoDto = JSONUtil.toBean(JSONUtil.toJsonStr(data), User.class);
            video.setAuthorName(userInfoDto.getName());
            video.setAuthorImage(userInfoDto.getImage());
            return video;
        }).collect(Collectors.toList());
        return R.success(list);
    }

    @Override
    public R<List<Video>> selectMyVideoByAudited() {
        List<Video> videos = query().eq("author_id", AppThreadLocalUtil.getUser().getId()).ge("status",2).list();
        List<Video> list = videos.stream().map(video -> {
            video.setIsLike(likeService.isLike(video.getId()));
            LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) userClient.information(video.getAuthorId()).getData();
            User userInfoDto = JSONUtil.toBean(JSONUtil.toJsonStr(data), User.class);
            video.setAuthorName(userInfoDto.getName());
            video.setAuthorImage(userInfoDto.getImage());
            return video;
        }).collect(Collectors.toList());
        return R.success(list);
    }

    @Override
    public R<List<Video>> selectMyVideoByUnPublish() {
        List<Video> videos = query().eq("author_id", AppThreadLocalUtil.getUser().getId()).eq("status",8).list();
        List<Video> list = videos.stream().map(video -> {
            video.setIsLike(likeService.isLike(video.getId()));
            LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) userClient.information(video.getAuthorId()).getData();
            User userInfoDto = JSONUtil.toBean(JSONUtil.toJsonStr(data), User.class);
            video.setAuthorName(userInfoDto.getName());
            video.setAuthorImage(userInfoDto.getImage());
            return video;
        }).collect(Collectors.toList());
        return R.success(list);
    }

    @Override
    public R<List<Video>> selectMyVideoByReleased() {
        List<Video> videos = query().eq("author_id", AppThreadLocalUtil.getUser().getId()).eq("status",9).list();
        List<Video> list = videos.stream().map(video -> {
            video.setIsLike(likeService.isLike(video.getId()));
            LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) userClient.information(video.getAuthorId()).getData();
            User userInfoDto = JSONUtil.toBean(JSONUtil.toJsonStr(data), User.class);
            video.setAuthorName(userInfoDto.getName());
            video.setAuthorImage(userInfoDto.getImage());
            return video;
        }).collect(Collectors.toList());
        return R.success(list);
    }

    @Resource
    private KafkaTemplate<String,String> kafkaTemplate;

    /**
     * 发送消息创建索引
     */
    private void createVideoESIndex(Video video, String content) {
        SearchVo vo = new SearchVo();
        BeanUtils.copyProperties(video,vo);
        vo.setContent(content);
        kafkaTemplate.send(VideoConstants.VIDEO_ES_SYNC_TOPIC, JSON.toJSONString(vo));
    }
}
